<?php
/**
 * Created by PhpStorm.
 * User: sofwu
 * Date: 2019/1/14
 * Time: 下午3:22
 */
namespace App\Http\Transformer;

use App\Model\Order;
use Illuminate\Support\Facades\Redis;
use League\Fractal\TransformerAbstract;

class OrderTransformer extends TransformerAbstract
{
    public function transform(Order $order)
    {
        return[
            'id'=> $order->id,
            'order_sn'=> $order->order_sn,
            'order_status'=> $order->order_status,
            'pay_status'=> $order->pay_status,
            'total_amount'=> $order->total_amount,
            'name'=> $order->name,
            'user_note'=> $order->user_note,
            'pay_time'=> $order->pay_time ? date('Y-m-d H:i:s', $order->pay_time) : '',
            'nice'=> Redis::scard('order:'.$order->id),
            'is_public'=> $order->is_public,
            'created_at'=> $order->created_at->format('Y-m-d H:i:s'),
            'user'=> [
                'id'=> $order->user->id,
                'name'=> $order->user->name,
                'sex'=> $order->user->sex,
                'head_pic'=> $order->user->head_pic,
                'nickname'=> $order->user->nickname,
            ],
            'good'=> [
                'name'=>$order->orderGoods[0]->goods->name,
                'pic'=> !$order->orderGoods[0]->goods->getFirstMediaUrl() ? '' :env('APP_URL').$order->orderGoods[0]->goods->getFirstMediaUrl(),
            ]

        ];
    }
}